<?php
define('FPDF_FONTPATH',$unocartConfig->path . 'pdf/font/');
define('FPDF_IMAGEPATH',$unocartConfig->path . 'pdf/');
error_reporting(0);
include_once ('ufpdf.php');
function add_n($s,$len) {
    $mas = explode("\n",$s);
    $k1 = count($mas);
    for ($i = 0; $i < $k1; $i++) {
        $l = Jstring::strlen($mas[$i]);
        $k2 = intval($l / $len);
        for ($j = 1; $j <= $k2; $j++) {
            $xx = substr($mas[$i],($j - 1) * $len,$len);
            $np[$j] = Jstring::strrpos($xx," ") + ($j - 1) * $len;
        }
        for ($j = 1; $j <= $k2; $j++) {
            $mas[$i] = subJstring::str_ireplace($mas[$i],"\n",$np[$j],1);
        }
    }
    $str = implode("\n",$mas);
    $encoding = Jstring::strtolower(Jstring::str_ireplace('charset=', '', _ISO)); 
	if ($encoding != 'utf-8'){
		require_once("charset/ConvertCharset.class.php");
		$converter = new ConvertCharset();
		$str = $converter->Convert($str, $encoding, 'utf-8');
	}
	return $str;
}

function getCountN($str){
	return (($count = substr_count($str, "\n")) > 1) ? ($count) : (++$count);
}

class my_UFPDF extends UFPDF{
	function addNewPage(){
		$this->addPage();
		$this->addTitleHead();	
	}
	function addTitleHead(){
		global $unocartConfig;
		$this->Image($unocartConfig->path . '/images/header.jpg',1,1,$unocartConfig->pdf_header_width,$unocartConfig->pdf_header_height);
		$this->Image($unocartConfig->path . '/images/footer.jpg',1,265,$unocartConfig->pdf_footer_width,$unocartConfig->pdf_footer_height);
	}
	
}

function generatePDF($order, $cart, $unocartConfig){
	$pdf = new my_UFPDF();
	$pdf->Open();
	$pdf->SetCompression(true);
	$pdf->AddFont('verdana','','verdana.php');
	$pdf->AddFont('verdanab','','verdanab.php');
	$pdf->AddFont('verdanai','','verdanai.php');
	$pdf->addNewPage();
	$pdf->SetFont('verdana','',8);
	$pdf->SetXY(155,12);
	$pdf->SetTextColor(255,255,255);
	$pdf->MultiCell(40,3,$unocartConfig->store_address . "\n" . $unocartConfig->store_zip . " " . $unocartConfig->store_city . "\n" . _UNOCART_CONTACT_PHONE . ":" . $unocartConfig->contact_phone . "\n" . _UNOCART_CONTACT_FAX . ":" . $unocartConfig->contact_fax,0,'R');
	$pdf->SetXY(20,55);
	$pdf->setfontsize(6);
	$pdf->SetTextColor(0,0,0);
	$pdf->MultiCell(80,3,$unocartConfig->store_company_name . ", " . $unocartConfig->store_address . ", " . $unocartConfig->store_zip . " " . $unocartConfig->store_city,0,'L');
	
	$pdf->SetXY(110,55);
	$pdf->SetFont('verdanab','',11);
	$pdf->SetTextColor(0,0,0);
	$pdf->MultiCell(80,3,_UNOCART_EMAIL_BILL,0,'R');
	
	$pdf->SetFont('verdana','',11);
	$pdf->SetXY(20,60);
	$pdf->MultiCell(80,4.5,$order->firma_name . "\n" . $order->f_name . " " . $order->l_name . "\n" . $order->street . "\n" . $order->zip . " " . $order->city . "\n" . $order->country,0,'L');
	
	$pdf->SetFont('verdanai','',11);
	$pdf->SetXY(110,65);
	$pdf->MultiCell(80,4.5,_UNOCART_ORDER_SHORT_NR . " " . $order->order_number . "\n" . _UNOCART_ORDER_FROM . " " . $order->order_date,0,'R');
	$pdf->SetDrawColor(0,0,0);
	$pdf->SetFont('verdana','',7);
	$pdf->SetXY(150,96);
	$pdf->setfillcolor(200,200,200);
	$pdf->Rect(20,115,170,5,'F');
	$pdf->SetFont('verdanab','',9);
	$pdf->SetXY(20,115);
	$pdf->MultiCell(130,5,_UNOCART_DESCRIPTION,1,'L');
	$pdf->SetXY(150,115);
	$pdf->MultiCell(40,5,_UNOCART_TOTAL, 1,'R');
	$offsetHeight = 0;
	$pdf->SetFont('verdana','',7);
	foreach ($cart->products as $prod){
		$descr = add_n($prod['product_name'], 105);
		if (isset($prod['attributes_value'])){
			$descr_attr = "";
			foreach ($prod['attributes_value'] as $attr){
				$descr_attr .= "\n" . $attr->attr . ": " . $attr->value . ""; 
			} 
		} else {
			$descr_attr = '';
		}
		$descr .= add_n($descr_attr, 105);
		$pdf->SetXY(20,122 + 7 * $offsetHeight);
		$count_n = getCountN($descr);
		$pdf->Rect(20,120 + 7 * $offsetHeight,170,7*$count_n);
		$pdf->Rect(20,120 + 7 * $offsetHeight,130,7*$count_n);
		$pdf->MultiCell(100,4,$descr,'0','L');
		$pdf->SetXY(170,122 + 7 * $offsetHeight);
		$param['price'] = $prod['quantity'] * $prod['price_netto'];
		$pdf->MultiCell(20,4,format_price($param),'0','R');
		$offsetHeight += $count_n;
		if ($offsetHeight > 15){
			$pdf->addNewPage();
			$offsetHeight = -8;	
		}
	}
	if ($offsetHeight > 10){
		$pdf->addNewPage();
		$offsetHeight = -8;	
	}
	
	
	//betrag
	$v = 120 + 7 * ($offsetHeight);
	$pdf->SetFont('verdanab','',10);
	
	
	$pdf->SetXY(20,$v);
	$pdf->Rect(20,$v,170,5,'F');
	$pdf->MultiCell(130,5,_UNOCART_SUBTOTAL_NETTO,'1','R');
	
	$pdf->SetXY(150,$v);
	$param['price'] = $order->netto_sum;
	$pdf->MultiCell(40,5,format_price($param),'1','R');
	

	$pdf->SetXY(20,$v + 5);
	$pdf->Rect(20,$v + 5,170,5,'F');
	$pdf->MultiCell(130,5,_UNOCART_SHIPPING_PRICE,'1','R');
	
	$pdf->SetXY(150,$v + 5);
	$param['price'] = $order->order_shipping - $order->order_shipping_tax;
	$pdf->MultiCell(40,5,format_price($param),'1','R');

	foreach ($order->different_tax as $percent=>$value){
		$param['price'] = $value;
		$str .= $percent . "% = " . format_price($param) . ", ";
	}
	$str = substr($str, 0, Jstring::strlen($str) - 2);
	
	$pdf->SetXY(20,$v + 10);
	$pdf->Rect(20,$v + 10,170,5,'F');
	$pdf->MultiCell(130,5,_UNOCART_TAX . " ($str)",'1','R');
	
	$pdf->SetXY(150,$v + 10);
	$param['price'] = $order->order_tax;
	$pdf->MultiCell(40,5,format_price($param),'1','R');
	
	
	$pdf->SetXY(20,$v + 15);
	$pdf->Rect(20,$v + 15,170,5,'F');
	$pdf->MultiCell(130,5,_UNOCART_TOTAL,'1','R');
	
	$pdf->SetXY(150,$v + 15);
	$param['price'] = $order->order_total;
	$pdf->MultiCell(40,5,format_price($param),'1','R');
	
	$v = 6 * $offsetHeight;
	$pdf->SetFont('verdana','',7);
	$pdf->SetXY(115,165 + $v);
	$pdf->Rect(115,165 + $v,75,4,'F');
	$pdf->MultiCell(75,4,_UNOCART_BENEF_BANK,'1','L');
	$pdf->SetXY(115,169 + $v);
	$pdf->MultiCell(75,4,$unocartConfig->benef_bank_info,'1','L');
	$pdf->SetXY(115,173 + $v);
	$pdf->MultiCell(75,4,_UNOCART_BENEF_BIC,'1','L');
	$pdf->SetXY(115,177 + $v);
	$pdf->MultiCell(75,4,_UNOCART_BENEF_CONTO,'1','L');
	
	$pdf->SetXY(115,181 + $v);
	$pdf->MultiCell(75,4,_UNOCART_BENEF_IBAN,'1','L');
	
	$pdf->SetXY(115,185 + $v);
	$pdf->MultiCell(75,4,_UNOCART_BENEF_SWIFT,'1','L');
	
	$pdf->Rect(115,189 + $v,75,4,'F');
	$pdf->SetXY(115,189 + $v);
	$pdf->MultiCell(75,4,_UNOCART_INTERM_BANK,'1','L');
	
	$pdf->SetXY(115,193 + $v);
	$pdf->MultiCell(75,4,_UNOCART_INTERM_NAME,'1','L');
	
	$pdf->SetXY(115,197 + $v);
	$pdf->MultiCell(75,4,_UNOCART_INTERM_SWIFT,'1','L');
	//r
	$pdf->SetXY(115,173 + $v);
	$pdf->MultiCell(75,4,$unocartConfig->benef_bic,'0','R');
	
	$pdf->SetXY(115,177 + $v);
	$pdf->MultiCell(75,4,$unocartConfig->benef_conto,'0','R');
	
	$pdf->SetXY(115,181 + $v);
	$pdf->MultiCell(75,4,$unocartConfig->benef_iban,'0','R');
	
	$pdf->SetXY(115,185 + $v);
	$pdf->MultiCell(75,4,$unocartConfig->benef_swift,'0','R');
	
	
	$pdf->SetXY(115,193 + $v);
	$pdf->MultiCell(75,4,$unocartConfig->interm_name,'0','R');
	
	$pdf->SetXY(115,197 + $v);
	$pdf->MultiCell(75,4,$unocartConfig->interm_swift,'0','R');
	
	$name_pdf = $order->order_id . "_" . md5(uniqid(rand(0,100))) . ".pdf";
	$pdf->Output($unocartConfig->path . "pdf/files/" . $name_pdf ,'F');
	return $name_pdf;
}
?>